package com.first.dao.impl;

import com.first.dao.RoleDao;
import com.first.entity.Role;
import com.first.util.DBUtil;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class RoleDaoImpl implements RoleDao {
    @Override
    public List<Map> queryAll(Role role) {
        String sql="select * from role limit ?,?";
        if(role.getLimit()==0){
            role.setLimit(this.queryAllCount());
            role.setPage(1);
        }
        return DBUtil.queryListMap(sql,(role.getPage() - 1) * role.getLimit(), role.getLimit());
    }

    @Override
    public int queryAllCount() {
        String sql="select count(*) from role";
        List<List> list = DBUtil.queryList(sql);
        return Integer.parseInt(list.get(0).get(0).toString());
    }

    @Override
    public int addRole(Role role) {
        String verify="select * from role where rname=?";
        List<List> list = DBUtil.queryList(verify, role.getRname());
        if(list.size()>0){
            return -2;
        }
        String sql="insert into role values(null,?,?,?)";
        return DBUtil.update(sql,role.getRname(),role.getMemo(),role.getStatus());
    }

    @Override
    public int updateRole(Role role) {
        String sql="update role set rname=?,memo=?,status=? where rid=?";
        return DBUtil.update(sql,role.getRname(),role.getMemo(),role.getStatus(),role.getRid());
    }

    @Override
    public int delRode(Integer rid) {
        String sql="delete from role where rid=?";
        return DBUtil.update(sql,rid);
    }
}
